package com.company.ndklearnproject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.company.ndklearnproject.bean.Dog;
import com.company.ndklearnproject.bean.Person;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private String name = "Toosan";
    private static int sAge = 24;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(String.valueOf(sAge));
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                changeStringFromJNI();
//                tv.setText(name);
                // 每次点击+1
                changeAgeFromJNI();
                callMethodFromJNI();
                tv.setText(String.valueOf(sAge));
                callGetDogFromJNI();
            }
        });

        initData();
        Person person = new Person("Jany", 18);
        Log.i(TAG, "JNI Java层打印原始person = " + person);
        callObjectFromJNI(person);
        Log.i(TAG, "JNI Java层打印JNI修改过的person = " + person);
        Log.i(TAG,"JNI 由C层构建出来的Person对象 \nperson = " + callGetObjectFromJNI());
        Dog dogJni =  callGetDogFromJNI();
        Log.i(TAG,"JNI 由C层构建出来的Dog对象 \ndogJni = " + dogJni);
    }

    private void initData() {
        int count = 10;
        String info = "传递给JNI";
        int[] numbers = {1, 2, 3, 4, 5};
        String[] objects = {"A", "B", "C", "D", "e"};
        callParamsFromJNI(count, info, numbers, objects);
        for (int i = 0; i < numbers.length; i++) {
            Log.i(TAG, "JNI initData number[" + i + "] = " + numbers[i]);
        }
    }

    public void printInfo() {
        Log.i(TAG, "供JNI调用的测试方法被调用");
        Toast.makeText(this, "被JIN调用了", Toast.LENGTH_SHORT).show();
    }

    public int getAge() {
        Log.i(TAG, "JNI通过函数获取age");
        return sAge;
    }

    @Override
    protected void onDestroy() {
        callDeleteDogFromJNI();
        super.onDestroy();
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    /**
     * 初步使用JNI
     * 改变MainActivity中的一个属性的值 如 name
     */
    public native void changeStringFromJNI();

    /**
     * 修改int类型的静态变量
     */
    public native void changeAgeFromJNI();

    /**
     * JNI调用Java中的函数
     */
    public native void callMethodFromJNI();

    /**
     * 各类型的参数传递
     */
    public native void callParamsFromJNI(int count, String info, int[] numbers, String[] objects);

    /**
     * 传一个Java自己定义的对象给JNI,JNI修改给对象的属性，以及该对象的方法调用
     *
     * @param person person
     */
    public native void callObjectFromJNI(Person person);

    /**
     * 在C层构建一个Person，并set一个Student到Person中
     */
    public native Person callGetObjectFromJNI();

    /**
     * 通过构造方法构建对象
     */
    public native Dog callGetDogFromJNI();

    /**
     * 释放Jni层的全局引用Dog
     */
    public native void callDeleteDogFromJNI();

}